package org.xxx.epms.models.epms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.xxx.epms.common.api.CommonPage;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.models.epms.dto.*;
import org.xxx.epms.models.epms.model.AqiMeasuredResult;
import org.xxx.epms.models.epms.model.GridProvince;
import org.xxx.epms.models.epms.service.AqiMeasuredResultService;
import org.xxx.epms.models.epms.service.GridProvinceService;
import org.xxx.epms.models.epms.service.InspectorService;
import org.xxx.epms.models.epms.service.MonitorService;

import java.awt.print.PageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lb
 * @since 2024-06-08
 */
@RestController
@RequestMapping("/epms/aqiMeasuredResult")
public class AqiMeasuredResultController {

    @Autowired
    private AqiMeasuredResultService aqiMeasuredResultService;
    @Autowired
    private InspectorService inspectorService;
    @Autowired
    private MonitorService monitorService;
    @Autowired
    private GridProvinceService gridProvinceService;

    //获取反馈列表总页数
    @RequestMapping(value = "/getMeasuredResultListTotalPage",method = RequestMethod.POST )
    @ApiOperation(value = "获取确认列表总页数", notes = "获取确认列表总页数")
    public CommonResult<Integer> getMeasuredResultListTotalPage(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        int totalPage = aqiMeasuredResultService.getTotalPage(aqiMeasuredResultParam, pageSize);
        System.out.println(totalPage);
        return CommonResult.success(totalPage);
    };

    //分页获取反馈列表
    @RequestMapping(value="/getMeasuredResultList", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取确认列表", notes = "分页获取确认列表")
    public CommonResult<CommonPage<AqiMeasuredResult>> getMeasuredResultList(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiMeasuredResult> page = aqiMeasuredResultService.getPageList(aqiMeasuredResultParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @RequestMapping(value="/getMeasuredResultDetail", method = RequestMethod.POST)
    @ApiOperation(value = "获取带有网格员信息的确认详情", notes = "获取带有网格员信息的确认详情")
    public CommonResult<CommonPage<AqiMeasuredResultDetailDTO>> getMeasuredResultDetail(@RequestBody AqiMeasuredResultParam aqiMeasuredResultParam,
                                                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiMeasuredResult> aqiMeasuredResultPage = aqiMeasuredResultService.getPageList(aqiMeasuredResultParam, pageSize, pageNum);
        System.out.println(aqiMeasuredResultPage.getRecords());
        Page<AqiMeasuredResultDetailDTO> page = new Page<>();
        List<AqiMeasuredResultDetailDTO> list = new ArrayList<>();
        for (AqiMeasuredResult aqiMeasuredResult : aqiMeasuredResultPage.getRecords()) {
            AqiMeasuredResultDetailDTO dto = new AqiMeasuredResultDetailDTO();
            dto.setAqiMeasuredResult(aqiMeasuredResult);
            dto.setAqiValue(aqiMeasuredResultService.getAqiByAqimrId(aqiMeasuredResult.getAqimrId()));
            dto.setInspector(inspectorService.getInspectorById(aqiMeasuredResult.getInsId()));
            dto.setMonitor(monitorService.getMonitorByMonId(aqiMeasuredResult.getMonId()));
            list.add(dto);
        }
        page.setRecords(list);
        System.out.println(list);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("获取所有省份的AQI超标结果")
    @RequestMapping(value = "/listAqiMeasuredResultByProvinces", method = RequestMethod.POST)
    public CommonResult<List<ProvinceCountDTO>> listAqiMeasuredResultByProvinces(){
        List<ProvinceCountDTO> provinceCountDTOList = new ArrayList<>();
        List<GridProvince> gridProvinceList = gridProvinceService.getAllGridProvinces();
        for (GridProvince gridProvince : gridProvinceList) {
            ProvinceCountDTO provinceCountDTO = aqiMeasuredResultService.getAqiMeasuredResultByProId(gridProvince.getProvinceId()) !=null ?
                   aqiMeasuredResultService.getAqiMeasuredResultByProId(gridProvince.getProvinceId()) : new ProvinceCountDTO();
            provinceCountDTO.setProvinceId(gridProvince.getProvinceId());
            provinceCountDTO.setProvinceName(gridProvince.getProvinceName());
            provinceCountDTO.setProvinceAbbr(gridProvince.getProvinceAbbr());
            provinceCountDTOList.add(provinceCountDTO);
        }
        return CommonResult.success(provinceCountDTOList);
    }


    @ApiOperation("获取AQI测量结果分布情况")
    @RequestMapping(value = "/getAqiDistribution", method = RequestMethod.GET)
    public CommonResult<List<AqiDistributionPageDTO>> getAqiDistribution() {
        return CommonResult.success(aqiMeasuredResultService.getAqiDistribution());
    }

    @ApiOperation("AQI空气质量指数超标趋势")
    @RequestMapping(value = "/getAqiTrend", method = RequestMethod.GET)
    public CommonResult<List<AqiTrendResponseDTO>> getAqiTrend() {
        return CommonResult.success(aqiMeasuredResultService.getAqiTrend());
    }

    @ApiOperation("获取AQI已测试结果数")
    @RequestMapping(value = "/getAqiMeasuredResultCount", method = RequestMethod.GET)
    public CommonResult<Integer> getAqiMeasuredResultCount() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultCount());
    }

    @ApiOperation("获得AQI空气质量指数良好总数")
    @RequestMapping(value = "/getAqiGoodCount", method = RequestMethod.GET)
    public CommonResult<Integer> getAqiGoodCount() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultGoodCount());
    }

    @ApiOperation("获得AQI省份覆盖率")
    @RequestMapping(value = "/getAqiProvinceCoverage", method = RequestMethod.GET)
    public CommonResult<String> getAqiProvinceCoverage() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultProvinceCov());
    }


    @ApiOperation("获得AQI城市覆盖率")
    @RequestMapping(value = "/getAqiCityCoverage", method = RequestMethod.GET)
    public CommonResult<String> getAqiCityCoverage() {
        return CommonResult.success(aqiMeasuredResultService.getAqiMeasuredResultCityCov());
    }

    @ApiOperation("其他数据统计")
    @RequestMapping(value = "/getOtherData", method = RequestMethod.GET)
    public CommonResult<OtherDataResponseDTO> getOtherData() {
        OtherDataResponseDTO otherData = new OtherDataResponseDTO();
        otherData.setCount(aqiMeasuredResultService.getAqiMeasuredResultCount());
        otherData.setGoodCount(aqiMeasuredResultService.getAqiMeasuredResultGoodCount());
        otherData.setProvinceCoverage(aqiMeasuredResultService.getAqiMeasuredResultProvinceCov()+"%");
        otherData.setCityCoverage(aqiMeasuredResultService.getAqiMeasuredResultCityCov()+"%");
        return CommonResult.success(otherData);
    }

    @ApiOperation("获取反馈检测信息")
    @RequestMapping(value = "/getMeasuredResultByFbId", method = RequestMethod.GET)
    public CommonResult<AqiMeasuredResultDetailDTO> getMeasuredResultByFbId(@RequestParam(value = "fbId") Integer fbId){
        AqiMeasuredResultDetailDTO dto = aqiMeasuredResultService.getMeasuredResultByFbId(fbId);
        if(dto.getAqiMeasuredResult().getAqimrState() == 1){
            dto.setAqiValue(aqiMeasuredResultService.getAqiByAqimrId(dto.getAqiMeasuredResult().getAqimrId()));
        }else {
            dto.setAqiValue(null);
        }
        return CommonResult.success(dto);
    }
}

